/**  
 * @Title: TaskServiceImpl.java
 * @Package com.xzit.tmms.service.task.impl
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月27日 上午9:01:59
 * @version V1.0  
 */
package com.xzit.tmms.service.task.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xzit.tmms.bean.task.Meterial;
import com.xzit.tmms.bean.task.Task;
import com.xzit.tmms.bean.unit.Course;
import com.xzit.tmms.common.exception.ServiceException;
import com.xzit.tmms.common.page.Page;
import com.xzit.tmms.mapper.task.MeterialMapper;
import com.xzit.tmms.mapper.task.ModelMapper;
import com.xzit.tmms.mapper.task.TaskMapper;
import com.xzit.tmms.mapper.unit.CourseMapper;
import com.xzit.tmms.service.task.TaskService;

/**
 * @ClassName: TaskServiceImpl
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月27日 上午9:01:59
 */
@Service("taskService")
public class TaskServiceImpl implements TaskService {

	@Resource
	private TaskMapper taskMapper;

	@Resource
	private ModelMapper modelMapper;

	@Resource
	private CourseMapper courseMapper;

	@Resource
	private MeterialMapper meterialMapper;

	@Override
	public void addTask(Task task) throws ServiceException {
		try {
			// 插入任务
			taskMapper.save(task);
			Integer taskId = task.getTaskId();
			// 获取课程类型
			Course course = courseMapper.getById(task.getCourseId());
			// 获取课程模板列表
			String modelType = course.getModelType();
			List<Map<String, Object>> models = modelMapper.getModelByType(modelType);
			// 插入教学任务计划对应的模板材料记录
			for (Map<String, Object> model : models) {
				Integer modelId = Integer.parseInt(model.get("modelId").toString());
				String modelName = model.get("modelName").toString();
				Meterial meterial = new Meterial();
				meterial.setMeteName(modelName);
				meterial.setModelId(modelId);
				meterial.setTaskId(taskId);
				meterial.setMeteType(modelType);
				meterialMapper.save(meterial);
			}
		} catch (Exception e) {
			throw new ServiceException("保存教学任务时发生异常");
		}
	}

	@Override
	public Page<Map<String, Object>> queryTask(Page<Map<String, Object>> page, Task task) throws Exception {
		// 获取任务属性参数
		Map<String, Object> query = new HashMap<>();
		query.put("classId", task.getClassId());
		query.put("taskYear", task.getTaskYear());
		query.put("taskTerm", task.getTaskTerm());
		query.put("courseId", task.getCourseId());
		// 执行查询
		page.setQuery(query);
		taskMapper.queryTask(page);
		return page;
	}

	@Override
	public void deleteTask(Integer taskId) throws ServiceException {
		try {
			taskMapper.delete(taskId);
		} catch (Exception e) {
			throw new ServiceException("删除教学任务失败");
		}
	}

	@Override
	public Task geTaskById(Integer taskId) throws ServiceException {
		try {
			return taskMapper.getById(taskId);
		} catch (Exception e) {
			throw new ServiceException("查询任务异常");
		}
	}

	@Override
	public void updateTask(Task task) throws ServiceException {
		try {
			taskMapper.update(task);
		} catch (Exception e) {
			throw new ServiceException("更新任务异常");
		}
	}

	@Override
	public Map<String, Object> queryTaskDetail(Integer taskId) throws ServiceException {
		try {
			return taskMapper.queryTaskDetail(taskId);
		} catch (Exception e) {
			throw new ServiceException("查询任务详情异常");
		}
	}
}
